package com.yourcompany.web.controller;

import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yourcompany.api.dto.RegionQueryDTO;
import com.yourcompany.api.entity.SysRegion;
import com.yourcompany.api.service.SysRegionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "地区管理")
@RestController
@RequestMapping("/api/region")
public class SysRegionController {

    @Autowired
    private SysRegionService regionService;

    @Operation(summary = "分页查询地区")
    @GetMapping("/page")
    public SaResult pageRegions(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            RegionQueryDTO queryDTO) {
        Page<SysRegion> page = new Page<>(pageNum, pageSize);
        return SaResult.ok().setData(regionService.pageRegions(page, queryDTO));
    }

    @Operation(summary = "获取地区树")
    @GetMapping("/tree")
    public SaResult getRegionTree(RegionQueryDTO queryDTO) {
        return SaResult.ok().setData(regionService.getRegionTree(queryDTO));
    }

    @Operation(summary = "获取子地区")
    @GetMapping("/children/{parentId}")
    public SaResult getChildren(@PathVariable Long parentId) {
        return SaResult.ok().setData(regionService.getChildren(parentId));
    }

    @Operation(summary = "新增地区")
    @PostMapping
    public SaResult addRegion(@Validated @RequestBody SysRegion region) {
        regionService.addRegion(region);
        return SaResult.ok();
    }

    @Operation(summary = "修改地区")
    @PutMapping
    public SaResult updateRegion(@Validated @RequestBody SysRegion region) {
        regionService.updateRegion(region);
        return SaResult.ok();
    }

    @Operation(summary = "删除地区")
    @DeleteMapping("/{id}")
    public SaResult deleteRegion(@PathVariable Long id) {
        regionService.deleteRegion(id);
        return SaResult.ok();
    }
} 